package com.fqgj.xjd.user.service.impl;

import com.fqgj.common.api.Response;
import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.exception.common.ApplicationException;
import com.fqgj.xjd.user.client.UserHighmarkService;
import com.fqgj.xjd.user.client.enums.UserHighmarkStatusEnum;
import com.fqgj.xjd.user.client.request.UserHighmarkVo;
import com.fqgj.xjd.user.dao.UserAuthHighmarkDao;
import com.fqgj.xjd.user.entity.UserAuthHighmarkEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/10/12
 * Time: 下午3:00
 */
@Service("userHighmarkService")
public class UserHighmarkServiceImpl implements UserHighmarkService {

    @Autowired
    private UserAuthHighmarkDao userAuthHighmarkDao;

    /**
     * High Mark
     *
     * @param userHighmarkVo
     * @return
     */
    @Override
    public Response<UserHighmarkVo> addUserHighmark(UserHighmarkVo userHighmarkVo) {
        if (StringUtils.isEmpty(userHighmarkVo.getOrderId()) || StringUtils.isEmpty(userHighmarkVo.getUserCode())) {
            throw new ApplicationException("OrderId is empty!");
        }
        UserAuthHighmarkEntity userAuthHighmarkEntity = userAuthHighmarkDao.selectUserAuthHighmarkByOrderId(userHighmarkVo.getUserCode(), userHighmarkVo.getOrderId());
        if (userAuthHighmarkEntity == null) {
            userAuthHighmarkEntity = new UserAuthHighmarkEntity();
            BeanUtils.copyProperties(userHighmarkVo, userAuthHighmarkEntity);
            userAuthHighmarkDao.insert(userAuthHighmarkEntity);
        } else {
            userAuthHighmarkEntity.setStage(userHighmarkVo.getStage());
            userAuthHighmarkEntity.setStatus(userHighmarkVo.getStatus());
            userAuthHighmarkEntity.setReportData(userHighmarkVo.getReportData());
            userAuthHighmarkEntity.setDataFrom(userHighmarkVo.getDataFrom());
            userAuthHighmarkEntity.setCreateDate(userHighmarkVo.getCreateDate());
            if (StringUtils.isEmpty(userAuthHighmarkEntity.getProcessLogs())) {
                userAuthHighmarkEntity.setProcessLogs(userHighmarkVo.getProcessLogs());
            } else {
                userAuthHighmarkEntity.setProcessLogs(userAuthHighmarkEntity.getProcessLogs() + "," + userHighmarkVo.getProcessLogs());
            }
            userAuthHighmarkDao.updateByPrimaryKey(userAuthHighmarkEntity);
        }
        return Response.ok().putData(userHighmarkVo);
    }

    /**
     * 取最后一条
     *
     * @param userCode
     * @return
     */
    @Override
    public Response<UserHighmarkVo> getUserHighmarkByUserCode(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            throw new ApplicationException("UserCode is empty!");
        }
        UserAuthHighmarkEntity userAuthHighmarkEntity = userAuthHighmarkDao.selectLastUserAuthHighmark(userCode);
        if (userAuthHighmarkEntity == null) {
            return Response.ok();
        }
        UserHighmarkVo userHighmarkVo = new UserHighmarkVo();
        BeanUtils.copyProperties(userAuthHighmarkEntity, userHighmarkVo);
        return Response.ok().putData(userHighmarkVo);
    }

    /**
     * 根据pan查询数据 取最后一条
     *
     * @param panCardNo
     * @param isOriginal 是否取原始记录
     * @return
     */
    @Override
    public Response<UserHighmarkVo> getUserHighmarkByPanCardNo(String panCardNo, boolean isOriginal) {
        if (StringUtils.isEmpty(panCardNo)) {
            throw new ApplicationException("panCardNo is empty!");
        }
        UserAuthHighmarkEntity userAuthHighmarkEntity = userAuthHighmarkDao.selectLastUserAuthHighmarkByPanCardNo(panCardNo, isOriginal);
        if (userAuthHighmarkEntity == null) {
            return Response.ok();
        }
        UserHighmarkVo userHighmarkVo = new UserHighmarkVo();
        BeanUtils.copyProperties(userAuthHighmarkEntity, userHighmarkVo);
        return Response.ok().putData(userHighmarkVo);
    }

    /**
     * 查询
     *
     * @param orderId
     * @return
     */
    @Override
    public Response<UserHighmarkVo> getUserHighmarkByOrderId(String orderId) {
        if (StringUtils.isEmpty(orderId)) {
            throw new ApplicationException("OrderId is empty!");
        }
        UserAuthHighmarkEntity userAuthHighmarkEntity = userAuthHighmarkDao.selectUserAuthHighmarkByOrderId(null, orderId);
        if (userAuthHighmarkEntity == null) {
            return Response.ok();
        }
        UserHighmarkVo userHighmarkVo = new UserHighmarkVo();
        BeanUtils.copyProperties(userAuthHighmarkEntity, userHighmarkVo);
        return Response.ok().putData(userHighmarkVo);
    }

    /**
     * @param userCode
     * @return
     */
    @Override
    public Response<Boolean> isHighmarkFrozen(String userCode) {
        if (StringUtils.isEmpty(userCode)) {
            throw new ApplicationException("UserCode is empty!");
        }
        List<UserAuthHighmarkEntity> entityList = userAuthHighmarkDao.selectLast3Record(userCode);
        if (CollectionUtils.isEmpty(entityList) || entityList.size() < 3) {
            return Response.ok().putData(false);
        }
        boolean flag = true;//最后3条是否都是失败
        for (UserAuthHighmarkEntity entity : entityList) {
            if (UserHighmarkStatusEnum.SUCCESS.getStatus().equals(entity.getStatus())) {
                flag = false;
            }
        }
        if (flag) {
            //冻结规则：最后三次记录，1.gmtCreate和3.gmtCreate相差3天以内，并且1.gmtCreate 距离当前时间小于等于15天
            if (DateUtil.diffDate(entityList.get(0).getGmtCreate(), entityList.get(entityList.size() - 1).getGmtCreate()) <= 3) {
                if (DateUtil.diffDate(new Date(), entityList.get(0).getGmtCreate()) <= 15) {
                    return Response.ok().putData(true);
                }
            }
        }
        return Response.ok().putData(false);
    }
}
